package com.heima.wemedia.feign;

import com.heima.apis.user.IUserClient;
import com.heima.apis.wemedia.IWeMediaClient;
import com.heima.model.admin.dtos.AuthDto;
import com.heima.model.admin.dtos.NewsAuthDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.wemedia.dtos.AdWmChannelDto;
import com.heima.model.wemedia.dtos.SensitiveDto;
import com.heima.model.wemedia.dtos.WmChannelDto;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.wemedia.mapper.WmUserMapper;
import com.heima.wemedia.service.WmChannelService;
import com.heima.wemedia.service.WmNewsService;
import com.heima.wemedia.service.WmSensitiveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@Slf4j
@RestController
public class WemediaClient implements IWeMediaClient {
    @Autowired
    private WmSensitiveService wmSensitiveService;
    @Autowired
    private WmChannelService channelService;
    @Autowired
    private WmNewsService wmNewsService;
    @Autowired
    private WmUserMapper wmUserMapper;
    @PostMapping("/api/v1/admin/list")
    public ResponseResult list(@RequestBody AdWmChannelDto dto){return channelService.listChannel(dto);}


    @PostMapping("/api/v1/admin/save")
    public ResponseResult saveChannel(@RequestBody WmChannelDto wmChannel){return channelService.saveChannel(wmChannel);}

    @PostMapping("/api/v1/admin/update")
    @Override
    public ResponseResult updateChannel(WmChannelDto wmChannelDto) {
        return channelService.updateChannel(wmChannelDto);
    }
    @GetMapping("/api/v1/admin/del/{id}")
    @Override
    public ResponseResult delChannel(@PathVariable Integer id) {
        return channelService.delChannel(id);
    }

    @Override
    @PostMapping("/api/v1/news/list_vo")
    public ResponseResult wmCheckPageList(@RequestBody NewsAuthDto newsAuthDto) {
        return wmNewsService.wmCheckPageList(newsAuthDto);
    }

    @Override
    @PostMapping("/api/v1/news/one_vo/{id}")
    public ResponseResult detail(@PathVariable Integer id) {
        return wmNewsService.detail(id);
    }

    @Override
    @PostMapping("/api/v1/news/auth_fail")
    public ResponseResult checkFail(@RequestBody NewsAuthDto newsAuthDto) {
        return wmNewsService.checkFail(newsAuthDto);
    }

    @Override
    @PostMapping("/api/v1/news/auth_pass")
    public ResponseResult checkPass(@RequestBody NewsAuthDto newsAuthDto) {
        return wmNewsService.checkPass(newsAuthDto);
    }

    @Override
    public ResponseResult commentScan() {
        return wmSensitiveService.commentScan();
    }


    @Override
    @PostMapping("/api/v1/sensitive/list")
    public ResponseResult findSensitive(SensitiveDto dto) {
        return wmSensitiveService.findSensitive(dto);
    }

    @Override
    @PostMapping("/api/v1/sensitive/save")
    public ResponseResult saveSensitive(WmSensitive wmSensitive) {
        wmSensitive.setCreatedTime(new Date());
        wmSensitiveService.save(wmSensitive);
        return ResponseResult.okResult(wmSensitive);
    }

    @Override
    @DeleteMapping("/api/v1/sensitive/del/{id}")
    public ResponseResult deleteSensitive(Integer id) {
        wmSensitiveService.removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    @PostMapping("/api/v1/sensitive/update")
    public ResponseResult updateSensitive(WmSensitive wmSensitive) {
        wmSensitiveService.updateById(wmSensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
    @Autowired
    private IUserClient userClient;
    @PostMapping("/api/v1/auth/authPass")
    ResponseResult authPass(@RequestBody AuthDto dto){
        ApUser apUser = (ApUser) userClient.authPass(dto).getData();
        WmUser wmUser = wmUserMapper.selectById(dto.getId());
        wmUser.setId(dto.getId());
        wmUser.setName(apUser.getName());
        wmUser.setPassword(apUser.getPassword());
        wmUser.setPhone(apUser.getPhone());
        wmUser.setSalt(apUser.getSalt());
        wmUser.setStatus(apUser.getStatus());
        wmUserMapper.updateById(wmUser);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}